package com.xzkj.xiaozhengblog;

import java.io.UnsupportedEncodingException;
import java.sql.*;

import static com.xzkj.xiaozhengblog.main.getConn;

public class UserLogin {


    /** 用户注册
    * @param UserName 用户名
    * @param PassWord 用户密码
    * @param ip 用户IP
    * @param Email 用户名绑定邮箱
    * @param QQ 用户QQ
    * @return 注册成功返回"注册成功!",注册失败返回失败内容*/

    public static String regisetUser(String UserName, String PassWord, String ip, String Email, String QQ){
        if (isUserAccount(UserName)){
            return "已经存在用户:" + UserName;
        }
        //创建注册语句
        String sql = "INSERT INTO user (id,username,userpass,email,qq,ip) VALUES (null,?,?,?,?,?)";
        //创建ps
        PreparedStatement ps = null;
        try {
            ps = getConn().prepareStatement(sql);
        } catch (SQLException e) {
            e.printStackTrace();
            return "服务器内部SQL错误";
        }
        try {
            assert ps != null;
            ps.setString(1,UserName);
            ps.setString(2,PassWord);
            ps.setString(3,Email);
            ps.setString(4,QQ);
            ps.setString(5,ip);
            ps.executeUpdate();
        } catch (SQLException e){
            e.printStackTrace();
            return "服务器内部SQL错误";
        }
        return "注册成功!";
    }

    /** 判断用户账号是否存在
     * @param UserName 用户账号名
     * @return 存在返回true|不存在返回false*/

    public static boolean isUserAccount(String UserName){
        //创建查询语句
        String sql = "SELECT id FROM user WHERE username = ?";
        //创建ps
        PreparedStatement ps = null;
        try {
            ps = getConn().prepareStatement(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            assert ps != null;
            ps.setString(1,UserName);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        ResultSet rs = null;
        try {
            rs = ps.executeQuery();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        //光标只像最后一行
        try {
            assert rs != null;
            rs.last();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        //获取当前行
        int rowCount = 0;
        try {
            rowCount = rs.getRow();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if (rowCount == 1) {
            //存在返回True
            return true;
        }else {
            //不存在返回False
            return false;
        }
    }


    /** 用户登录
     * @param UserName 用户名
     * @param PassWord 用户密码
     * @param ip 用户IP
     * @return 登录成功返回用户标识|是否返回登录失败原因*/

    public static String loginUser(String UserName, String PassWord, String ip){
        //
        String sql = "SELECT id FROM user WHERE username = ? AND userpass = ?";
        PreparedStatement ps = null;
        try {
            ps = datamysql.conn.prepareStatement(sql);
            ps.setString(1,UserName);
            ps.setString(2,PassWord);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        ResultSet rs;
        int rowCount;
        try {
            assert ps != null;
            rs = ps.executeQuery();
            rs.last();
            rowCount = rs.getRow();
        } catch (SQLException e) {
            e.printStackTrace();
            return "服务器内部SQL错误";
        }
        if (rowCount == 1) {
            //满足登录,进行登录
            try {
                return String.valueOf(rs.getInt("id"));
            } catch (SQLException e) {
                e.printStackTrace();
                return "登录成功,但是唯一ID位NULL";
            }
        }else {
            //账号密码错误
            return "账号密码错误";
        }


    }

}
